<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

declare (strict_types=1);

namespace app\finance\controller;

use app\base\BaseController;
use app\user\model\DepartmentChange;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
use app\user\model\Admin as AdminList;

class Report extends BaseController
{
    public function yg_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $start_date = $param['start_date'] ?? '';
            $end_date = $param['end_date'] ?? '';
            $where[] = ['status', '<', 2];
            $where[] = ['id', '<>', 1];
            $uids = Db::name('admin')->where($where)->column('id');
            $zt = $this->performance($uids, $start_date, $end_date);
            $admin = AdminList::where($where)
                ->field('id,did,name,base_salary,insurance_deduct')
                ->order('id desc')
                ->select()->toArray();
            foreach ($admin as $k => $v) {
                $admin[$k]['department'] = Db::name('Department')->where(['id' => $v['did']])->value('title');
                $res = $this->performance([$v['id']], $start_date, $end_date);
                $admin[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $admin[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $admin[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);

                $gxl = ($admin[$k]['lirun'] / (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0))) * 100;
                $admin[$k]['gxl'] = round($gxl, 2) . ' %';
            }
            $data['data'] = $admin;
            return table_assign(0, '', $data);
        } else {
            return view();
        }
    }


    public function bm_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $start_date = $param['start_date'] ?? '';
            $end_date = $param['end_date'] ?? '';
            $where[] = ['status', '=', 1];
            $uids = Db::name('admin')->where($where)->column('id');
            $zt = $this->performance($uids, $start_date, $end_date);
            $admin = Db::name('department')->where($where)
                ->field('id,title as department')
                ->order('id asc')
                ->select()->toArray();
            foreach ($admin as $k => $v) {
                $uids = Db::name('admin')->where(['did' => $v['id']])->column('id');
                $res = $this->performance($uids, $start_date, $end_date);
                $admin[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $admin[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $admin[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);

                $gxl = ($admin[$k]['lirun'] / (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0))) * 100;
                $admin[$k]['gxl'] = round($gxl, 2) . ' %';
            }
            $data['data'] = $admin;
            return table_assign(0, '', $data);
        } else {
            return view();
        }
    }


    public function zt_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $start_date = $param['start_date'] ?? '';
            $end_date = $param['end_date'] ?? '';
            $where[] = ['status', '<', 2];
            $where[] = ['id', '<>', 1];
            $uids = Db::name('admin')->where($where)->column('id');
            $zt = $this->performance($uids, $start_date, $end_date);
            $uids = Db::name('admin')->where($where)->column('id');
            $res = $this->performance($uids, $start_date, $end_date);
            $admin[0]['id'] = 1;
            $admin[0]['title'] = '卓越集团';
            $admin[0]['total_incomes'] = $res['total_incomes'] ?? 0;
            $admin[0]['total_payments'] = $res['total_payments'] ?? 0;
            $admin[0]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
            $gxl = ($admin[0]['lirun'] / (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0))) * 100;
            $admin[0]['gxl'] = round($gxl, 2) . ' %';
            $data['data'] = $admin;
            return table_assign(0, '', $data);
        } else {
            return view();
        }
    }


    private function performance($ids = [], $start = '', $end = '')
    {
//        if ($start) {
        $start = date('2023-12-01 00:00:00');
        $start = strtotime($start);
//        }

//        if ($end) {
        $end = date('Y-m-d 23:59:59');
        $end = strtotime($end);
//        }

        $income1 = $this->income_xinban($ids, $start, $end);
        $income2 = $this->income_binggou($ids, $start, $end);
        $income3 = $this->income_liepin($ids, $start, $end);
        $income4 = $this->income_baoshui($ids, $start, $end);
        $income5 = $this->income_wxkh($ids, $start, $end);
        $sum_incomes = $income1 + $income2 + $income3 + $income4 + $income5;

        $payment1 = $this->payment_binggou($ids, $start, $end);
        $payment2 = $this->payment_liepin($ids, $start, $end);
        $sum_payments = $payment1 + $payment2;

        return ['total_incomes' => $sum_incomes, 'total_payments' => $sum_payments];
    }

    private function income_xinban($ids, $start, $end)
    {
        $db = db::name('license_cert')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('license_payment b', 'a.id = b.cert_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($start) {
            $db->where('b.check_time', '>=', $start);
            $db->where('b.check_time', '<=', $end);
        }


        $deduct = $db->sum('b.money');
        return $deduct;
    }

    private function income_binggou($ids, $start, $end)
    {
        $db = db::name('transfer')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('transfer_playback b', 'a.id = b.transfer_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($start) {
            $db->where('b.check_time', '>=', $start);
            $db->where('b.check_time', '<=', $end);
        }
        $deduct = $db->sum('b.remit_account_money');
        return $deduct;
    }

    private function income_baoshui($ids, $start, $end)
    {
        $db = db::name('declare_tax')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('declare_tax_payment b', 'a.id = b.tax_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($start) {
            $db->where('b.check_time', '>=', $start);
            $db->where('b.check_time', '<=', $end);
        }
        $deduct = $db->sum('b.money');
        return $deduct;
    }

    private function income_wxkh($ids, $start, $end)
    {
        $db = db::name('security_account')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('security_account_payment b', 'a.id = b.account_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($start) {
            $db->where('b.check_time', '>=', $start);
            $db->where('b.check_time', '<=', $end);
        }
        $deduct = $db->sum('b.money');
        return $deduct;
    }


    private function income_liepin($ids, $start, $end, $type = 'U')
    {
        $db = db::name('bitjob_enterprise')
            ->where('a.delete_time', 0)
            ->where('a.battle_user_id', 'in', $ids)
            ->alias('a');
        $db->join('bitjob_payment b', 'a.id = b.bitjob_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($start) {
            $db->where('b.check_time', '>=', $start);
            $db->where('b.check_time', '<=', $end);
        }
        $deduct = $db->sum('b.money');

        return $deduct;
    }

    private function payment_binggou($ids, $start, $end)
    {
        $db = db::name('transfer')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('transfer_payment b', 'a.id = b.transfer_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($start) {
            $db->where('b.check_time', '>=', $start);
            $db->where('b.check_time', '<=', $end);
        }
        $deduct = $db->sum('b.remit_account_money');
        return $deduct;
    }

    private function payment_liepin($ids, $start, $end)
    {
        $db = db::name('bitjob_putpay')
            ->where('delete_time', 0)
            ->where('admin_id', 'in', $ids);
        $db->where('check_status', 2);
        if ($start) {
            $db->where('check_time', '>=', $start);
            $db->where('check_time', '<=', $end);
        }
        $deduct = $db->sum('total_money');
        return $deduct;
    }

    public function day_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $id = $param['id'] ?? '';
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y-%m-%d') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y-%m-%d')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $zt = $this->day_performance($uids, $v['day']);
                $res = $this->day_performance([$id], $v['day']);

                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $a = (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0));
                if ($a > 0) {
                    $gxl = ($item[$k]['lirun'] / $a) * 100;
                    $item[$k]['gxl'] = round($gxl, 2) . ' %';
                } else {
                    $item[$k]['gxl'] = '0 %';
                }
            };

            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            $name = $param['name'] ?? '';
            View::assign('id', $id);
            View::assign('name', $name);
            return view();
        }
    }

    public function dept_day_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $id = $param['id'] ?? '';
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y-%m-%d') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y-%m-%d')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $zt = $this->day_performance($uids, $v['day']);
                $dept_uids = Db::name('admin')->where('did', $id)->column('id');
                $res = $this->day_performance($dept_uids, $v['day']);

                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $a = (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0));
                if ($a > 0) {
                    $gxl = ($item[$k]['lirun'] / $a) * 100;
                    $item[$k]['gxl'] = round($gxl, 2) . ' %';
                } else {
                    $item[$k]['gxl'] = '0 %';
                }
            };

            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            View::assign('id', $id);
            $department = $param['department'] ?? '';
            View::assign('department', $department);
            return view();
        }
    }


    public function company_day_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y-%m-%d') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y-%m-%d')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $res = $this->day_performance($uids, $v['day']);
                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $item[$k]['gxl'] = '100 %';
            };

            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            View::assign('id', $id);
            return view();
        }
    }

    private function day_performance($ids = [], $day = '', $type = 'day')
    {
        $income1 = $this->day_income_xinban($ids, $day, $type);
        $income2 = $this->day_income_binggou($ids, $day, $type);
        $income3 = $this->day_income_liepin($ids, $day, $type);
        $income4 = $this->day_income_baoshui($ids, $day, $type);
        $income5 = $this->day_income_wxkh($ids, $day, $type);
        $sum_incomes = $income1 + $income2 + $income3 + $income4 + $income5;

        $payment1 = $this->day_payment_binggou($ids, $day, $type);
        $payment2 = $this->day_payment_liepin($ids, $day, $type);
        $sum_payments = $payment1 + $payment2;

        return ['total_incomes' => $sum_incomes, 'total_payments' => $sum_payments];
    }

    private function day_income_xinban($ids, $day, $type)
    {
        $db = db::name('license_cert')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('license_payment b', 'a.id = b.cert_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($type == 'day') {
            $db->where("from_unixtime(b.check_time, '%Y-%m-%d') = '$day'");
        } else if ($type == 'month') {
            $db->where("from_unixtime(b.check_time, '%Y-%m') = '$day'");
        } else if ($type == 'year') {
            $db->where("from_unixtime(b.check_time, '%Y') = '$day'");
        }
        $deduct = $db->sum('b.money');
        if ($deduct) {
            return $deduct;
        } else {
            return 0;
        }

    }

    private function day_income_binggou($ids, $day, $type)
    {
        $db = db::name('transfer')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('transfer_playback b', 'a.id = b.transfer_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($type == 'day') {
            $db->where("from_unixtime(b.check_time, '%Y-%m-%d') = '$day'");
        } else if ($type == 'month') {
            $db->where("from_unixtime(b.check_time, '%Y-%m') = '$day'");
        } else if ($type == 'year') {
            $db->where("from_unixtime(b.check_time, '%Y') = '$day'");
        }
        $deduct = $db->sum('b.remit_account_money');
        if ($deduct) {
            return $deduct;
        } else {
            return 0;
        }
    }

    private function day_income_baoshui($ids, $day, $type)
    {
        $db = db::name('declare_tax')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('declare_tax_payment b', 'a.id = b.tax_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($type == 'day') {
            $db->where("from_unixtime(b.check_time, '%Y-%m-%d') = '$day'");
        } else if ($type == 'month') {
            $db->where("from_unixtime(b.check_time, '%Y-%m') = '$day'");
        } else if ($type == 'year') {
            $db->where("from_unixtime(b.check_time, '%Y') = '$day'");
        }
        $deduct = $db->sum('b.money');
        if ($deduct) {
            return $deduct;
        } else {
            return 0;
        }
    }

    private function day_income_wxkh($ids, $day, $type)
    {
        $db = db::name('security_account')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('security_account_payment b', 'a.id = b.account_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($type == 'day') {
            $db->where("from_unixtime(b.check_time, '%Y-%m-%d') = '$day'");
        } else if ($type == 'month') {
            $db->where("from_unixtime(b.check_time, '%Y-%m') = '$day'");
        } else if ($type == 'year') {
            $db->where("from_unixtime(b.check_time, '%Y') = '$day'");
        }
        $deduct = $db->sum('b.money');
        if ($deduct) {
            return $deduct;
        } else {
            return 0;
        }
    }


    private function day_income_liepin($ids, $day, $type)
    {
        $db = db::name('bitjob_enterprise')
            ->where('a.delete_time', 0)
            ->where('a.battle_user_id', 'in', $ids)
            ->alias('a');
        $db->join('bitjob_payment b', 'a.id = b.bitjob_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($type == 'day') {
            $db->where("from_unixtime(b.check_time, '%Y-%m-%d') = '$day'");
        } else if ($type == 'month') {
            $db->where("from_unixtime(b.check_time, '%Y-%m') = '$day'");
        } else if ($type == 'year') {
            $db->where("from_unixtime(b.check_time, '%Y') = '$day'");
        }
        $deduct = $db->sum('b.money');

        if ($deduct) {
            return $deduct;
        } else {
            return 0;
        }
    }

    private function day_payment_binggou($ids, $day, $type)
    {
        $db = db::name('transfer')
            ->where('a.delete_time', 0)
            ->where('a.sign_id', 'in', $ids)
            ->field('a.*')
            ->alias('a');
        $db->join('transfer_payment b', 'a.id = b.transfer_id');
        $db->where('b.delete_time', 0);
        $db->where('b.check_status', 2);
        if ($type == 'day') {
            $db->where("from_unixtime(b.check_time, '%Y-%m-%d') = '$day'");
        } else if ($type == 'month') {
            $db->where("from_unixtime(b.check_time, '%Y-%m') = '$day'");
        } else if ($type == 'year') {
            $db->where("from_unixtime(b.check_time, '%Y') = '$day'");
        }
        $deduct = $db->sum('b.remit_account_money');
        if ($deduct) {
            return $deduct;
        } else {
            return 0;
        }
    }

    private function day_payment_liepin($ids, $day, $type)
    {
        $db = db::name('bitjob_putpay')
            ->where('delete_time', 0)
            ->where('admin_id', 'in', $ids);
        $db->where('check_status', 2);
        if ($type == 'day') {
            $db->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'");
        } else if ($type == 'month') {
            $db->where("from_unixtime(check_time, '%Y-%m') = '$day'");
        } else if ($type == 'year') {
            $db->where("from_unixtime(check_time, '%Y') = '$day'");
        }
        $deduct = $db->sum('total_money');
        if ($deduct) {
            return $deduct;
        } else {
            return 0;
        }
    }


    public function month_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $id = $param['id'] ?? '';
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y-%m') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y-%m')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $zt = $this->day_performance($uids, $v['day'], 'month');
                $res = $this->day_performance([$id], $v['day'], 'month');

                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $a = (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0));
                if ($a > 0) {
                    $gxl = ($item[$k]['lirun'] / $a) * 100;
                    $item[$k]['gxl'] = round($gxl, 2) . ' %';
                } else {
                    $item[$k]['gxl'] = '0 %';
                }
            };
            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            $name = $param['name'] ?? '';
            View::assign('id', $id);
            View::assign('name', $name);
            return view();
        }
    }

    public function dept_month_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $id = $param['id'] ?? '';
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y-%m') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y-%m')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $zt = $this->day_performance($uids, $v['day'], 'month');
                $dept_uids = Db::name('admin')->where('did', $id)->column('id');
                $res = $this->day_performance($dept_uids, $v['day'], 'month');

                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $a = (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0));
                if ($a > 0) {
                    $gxl = ($item[$k]['lirun'] / $a) * 100;
                    $item[$k]['gxl'] = round($gxl, 2) . ' %';
                } else {
                    $item[$k]['gxl'] = '0 %';
                }
            };

            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            View::assign('id', $id);
            $department = $param['department'] ?? '';
            View::assign('department', $department);
            return view();
        }
    }


    public function company_month_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y-%m') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y-%m')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $res = $this->day_performance($uids, $v['day'], 'month');
                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $item[$k]['gxl'] = '100 %';
            };

            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            View::assign('id', $id);
            return view();
        }
    }

    public function year_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $id = $param['id'] ?? '';
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $zt = $this->day_performance($uids, $v['day'], 'year');
                $res = $this->day_performance([$id], $v['day'], 'year');

                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $a = (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0));
                if ($a > 0) {
                    $gxl = ($item[$k]['lirun'] / $a) * 100;
                    $item[$k]['gxl'] = round($gxl, 2) . ' %';
                } else {
                    $item[$k]['gxl'] = '0 %';
                }
            };
            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            $name = $param['name'] ?? '';
            View::assign('id', $id);
            View::assign('name', $name);
            return view();
        }
    }

    public function dept_year_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $id = $param['id'] ?? '';
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $zt = $this->day_performance($uids, $v['day'], 'year');
                $dept_uids = Db::name('admin')->where('did', $id)->column('id');
                $res = $this->day_performance($dept_uids, $v['day'], 'year');

                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $a = (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0));
                if ($a > 0) {
                    $gxl = ($item[$k]['lirun'] / $a) * 100;
                    $item[$k]['gxl'] = round($gxl, 2) . ' %';
                } else {
                    $item[$k]['gxl'] = '0 %';
                }
            };

            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            View::assign('id', $id);
            $department = $param['department'] ?? '';
            View::assign('department', $department);
            return view();
        }
    }


    public function company_year_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $start = date('2023-12-01 00:00:00');
            $start = strtotime($start);
            $end = time();

            $db = db::name('admin_log');
            $db->field("from_unixtime(create_time, '%Y') as day");
            $db->where('create_time', '>=', $start);
            $db->where('create_time', '<=', $end);
            $db->group("from_unixtime(create_time, '%Y')");
            $db->order('day');
            $list = $db->paginate($rows);

            $item = $list->items();
            $data['total'] = $list->total();
            foreach ($item as $k => $v) {
                $where[] = ['status', '<', 2];
                $where[] = ['id', '<>', 1];
                $uids = Db::name('admin')->where($where)->column('id');
                $res = $this->day_performance($uids, $v['day'], 'year');
                $item[$k]['total_incomes'] = $res['total_incomes'] ?? 0;
                $item[$k]['total_payments'] = $res['total_payments'] ?? 0;
                $item[$k]['lirun'] = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $item[$k]['gxl'] = '100 %';
            };

            $data['data'] = $item;
            return table_assign(0, '', $data);
        } else {
            $param = get_params();
            $id = $param['id'] ?? '';
            View::assign('id', $id);
            return view();
        }
    }


    public function chart()
    {
        if (request()->isAjax()) {
            $start_date = $param['start_date'] ?? '';
            $end_date = $param['end_date'] ?? '';
            $where[] = ['status', '<', 2];
            $where[] = ['id', '<>', 1];
            $where[] = ['id', '<>', 3];
            $where[] = ['id', '<>', 33];
            $uids = Db::name('admin')->where($where)->column('id');
            $zt = $this->performance($uids, $start_date, $end_date);
            $admin = AdminList::where($where)
                ->field('id,name')
                ->order('id desc')
                ->select()->toArray();
            foreach ($admin as $k => $v) {
                $res = $this->performance([$v['id']], $start_date, $end_date);
                $lirun = ($res['total_incomes'] ?? 0) - ($res['total_payments'] ?? 0);
                $admin[$k]['gxl'] = ($lirun / (($zt['total_incomes'] ?? 0) - ($zt['total_payments'] ?? 0)));
            }

            $dates = array(); // 存放日期的数组
            for ($i=1; $i<=15; $i++) {
                $currentDate = date('Y-m-d', strtotime("-$i day")); // 当前日期的格式化字符串
                $dates[] = $currentDate; // 将日期添加到数组中
            }
            $yg = [];
            foreach ($dates as $k => $v){
                $yg[$k]['date'] = $v;
                $yg[$k]['count'] =  Db::name('admin')->where("from_unixtime(create_time, '%Y-%m-%d') = '$v'")->count();
            }

            $day = date('Y-m-d');
            $income1 = Db::name('license_payment')->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'")->sum('money');
            $income2 = Db::name('transfer_playback')->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'")->sum('remit_account_money');
            $income3 = Db::name('declare_tax_payment')->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'")->sum('money');
            $income4 = Db::name('security_account_payment')->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'")->sum('money');
            $income5 = Db::name('bitjob_payment')->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'")->sum('money');

            $psyment1 = Db::name('bitjob_putpay')->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'")->sum('total_money');
            $psyment2 = Db::name('transfer_payment')->where("from_unixtime(check_time, '%Y-%m-%d') = '$day'")->sum('remit_account_money');
            $inco = $income1+$income2+$income3+$income4+$income5;
            $pay = $psyment1+$psyment2;
            $total = ($inco+$pay);
            if($total==0){
                $sfk = [['name'=>'收款','money'=>0],['name'=>'付款','money'=>0]];
            }else{
                $sfk = [['name'=>'收款','money'=>$inco/($inco+$pay)*100],['name'=>'付款','money'=>$pay/($inco+$pay)*100]];
            }

            return table_assign(0, '',['data'=>['data_logs' => $admin,'yg'=>$yg,'sfk'=>$sfk]] );
        } else {
            return view();
        }

    }
}
